using System.Drawing;
using AutoClick.utils;
using Newtonsoft.Json.Linq;

namespace AutoClick.model.impl;

public class MoveAction(MatchResult matchResult, JObject action) : IAction
{
    public JObject Action { get; set; } = action;
    public MatchResult MatchResult { get; set; } = matchResult;

    public void DoAction()
    {
        JObject offset = SafeConvertUtil.ToJObject(Action["Offset"]);

        var anyMatch = SafeConvertUtil.ToJArray(Action["AnyMatch"]);
        foreach (var iconObj in anyMatch)
        {
            if (SafeConvertUtil.ObjToString(iconObj).Equals(MatchResult.DstIcon))
            {
                offset = SafeConvertUtil.ToJObject(Action["MatchedValue"]);
            }
        }

        // 目标程序RECT
        Rect rect = SafeConvertUtil.ToRect(Action["Rect"]);

        Point absPoint = CaptureUtil.GetAbsPoint(rect, MatchResult);

        absPoint.X += SafeConvertUtil.ToInt(offset["X"]);
        absPoint.Y += SafeConvertUtil.ToInt(offset["Y"]);

        // Console.WriteLine($"Move action: {absPoint} at: {DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}");
        MouseUtil.Move(absPoint);
    }
}